﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Module.Adsvertising.Lib;
using Framework.Module.Adsvertising.Lib.Zones;
using Framework.Module.Adsvertising.Lib.Data;
using Framework.Module.Adsvertising.Lib.DataAccess;

namespace Framework.Module.Adsvertising.Admin
{
    public partial class UpdateAdvertise : AuthenticatedControl
    {
        //string fileUpload = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                

                lstPositions.DataSource = PositionDB.GetAll();
                lstPositions.DataTextField = "Pos_Name";
                lstPositions.DataValueField = "Pos_ID";
                lstPositions.DataBind();

                SelectDate1.SetDate(DateTime.Now);
                SelectDate2.SetDate(DateTime.Now.AddDays(15));
            }           
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            lstZones.Items.Clear();
            if (Constants.InZone)
            {
                ZoneController.LoadZones(lstZones.Items, AppEnv.PortalId());
                lstZones.Enabled = true;
            }
            else
            {
                lstZones.Enabled = false; lstZones.SelectedIndex = 0;
            }
            lstZones.Items.Insert(0, new ListItem((String)this.GetLocalResourceObject("HomePage"), "0"));
            lstZones.SelectedValue = "0";
            LoadAdvertise();
        }

        private void LoadAdvertise()
        {
            int adID = ConvertUtility.ToInt32(Request.QueryString["advertiseid"]);
            if (adID != 0)
            {
                cmdAddNew.Enabled = false;
                cmdAddNext.Enabled = false;
                cmdEmpty.Enabled = false;
            }
            else adID = ConvertUtility.ToInt32(txtID.Text);
            AdvertiseInfo info = AdvertiseDB.GetInfo(adID);
            if (info == null)
            {
                cmdEmpty_Click(null, null); return;
            }
            txtID.Text = info.Advertise_ID.ToString();
            //FileUpload1.f = info.Advertise_Path;
            txtTarget.Text = info.Advertise_Target;

            txtName.Text = info.Advertise_Name;
            txtUrl.Text = info.Advertise_RedirectURL;
            txtWidth.Text = info.Advertise_Width.ToString();
            txtHeight.Text = info.Advertise_Height.ToString();
            chkEnable.Checked = info.Advertise_Enable;

            SelectDate1.SetDate(info.Advertise_StartDate);
            SelectDate2.SetDate(info.Advertise_EndDate);
            dropType.SelectedIndex = -1;
            MiscUtility.SetSelected(dropType.Items, info.Advertise_Type);

            lstPositions.SelectedIndex = -1;
            MiscUtility.SetSelected(lstPositions.Items, info.Advertise_PositionID.ToString());

            PositionInfo posInfo = PositionDB.GetInfo(info.Advertise_PositionID);
            if (txtWidth.Text == "0") txtWidth.Text = posInfo.Pos_Width.ToString();
            if (txtHeight.Text == "0") txtHeight.Text = posInfo.Pos_Height.ToString();

            foreach (ListItem item in lstZones.Items)
                if (Convert.ToString("|" + info.Advertise_Params).IndexOf("|" + item.Value + "|") >= 0)
                    item.Selected = true;
                else item.Selected = false;
            
        }
        
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            AdvertiseInfo info = new AdvertiseInfo();
            info.Advertise_Name = txtName.Text;
            info.Advertise_Enable = chkEnable.Checked;
            info.Advertise_Height = ConvertUtility.ToInt32(txtHeight.Text);
            info.Advertise_Width = ConvertUtility.ToInt32(txtWidth.Text);
            info.Advertise_StartDate = SelectDate1.GetDate();
            info.Advertise_EndDate = SelectDate2.GetDate();

            string filePath = "";
            if (FileUpload1.PostedFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath(Constants.UploadPath + CurrentUser.UserID)))
                {
                    Directory.CreateDirectory(Server.MapPath(Constants.UploadPath + CurrentUser.UserID));
                }
               
                filePath = Constants.UploadPath + CurrentUser.UserID + "/" + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
                FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
                info.Advertise_Path = filePath;
            }
            else
            {
                info.Advertise_Path = filePath;
            }
            info.Advertise_PositionID = ConvertUtility.ToInt32(lstPositions.SelectedValue);
            info.Advertise_Type = dropType.SelectedValue;
            info.Advertise_RedirectURL = txtUrl.Text;
            info.Advertise_Lang = AppEnv.GetLanguage();
            info.Advertise_Target = txtTarget.Text;
            string location = string.Empty;
            foreach (ListItem item in lstZones.Items)
            {
                if (item.Selected)
                {
                    location += item.Value + "|";
                }
            }
            info.Advertise_Params = location;
            txtID.Text = AdvertiseDB.Insert(info).ToString();
            try
            {

                lblStatusUpdate.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
            }
            catch
            {
                lblStatusUpdate.Text = (String)this.GetLocalResourceObject("UpdateError");
            }
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            AdvertiseInfo info = AdvertiseDB.GetInfo(ConvertUtility.ToInt32(txtID.Text));
            if (info == null)
            {
                cmdEmpty_Click(null, null); return;
            }
            info.Advertise_Name = txtName.Text;
            info.Advertise_Enable = chkEnable.Checked;
            info.Advertise_Height = ConvertUtility.ToInt32(txtHeight.Text);
            info.Advertise_Width = ConvertUtility.ToInt32(txtWidth.Text);
            info.Advertise_StartDate = SelectDate1.GetDate();
            info.Advertise_EndDate = SelectDate2.GetDate();

            string filePath = info.Advertise_Path;
            if (FileUpload1.PostedFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath(Constants.UploadPath + CurrentUser.UserID)))
                {
                    Directory.CreateDirectory(Server.MapPath(Constants.UploadPath + CurrentUser.UserID));
                }
                
                

                filePath = Constants.UploadPath + CurrentUser.UserID + "/" + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
                FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
                info.Advertise_Path = filePath;
            }
            else
            {
                info.Advertise_Path = filePath;
            }

            info.Advertise_PositionID = ConvertUtility.ToInt32(lstPositions.SelectedValue);
            info.Advertise_Type = dropType.SelectedValue;
            info.Advertise_RedirectURL = txtUrl.Text;
            info.Advertise_Lang = AppEnv.GetLanguage();
            info.Advertise_Target = txtTarget.Text;
            string location = string.Empty;
            foreach (ListItem item in lstZones.Items) if (item.Selected) location += item.Value + "|";
            info.Advertise_Params = location;

            try
            {
                AdvertiseDB.Update(info);
                lblStatusUpdate.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
            }
            catch
            {
                lblStatusUpdate.Text = (String)this.GetLocalResourceObject("UpdateError");
            }
        }

        protected void cmdEmpty_Click(object sender, EventArgs e)
        {
            txtName.Text = string.Empty;
            txtID.Text = string.Empty;
            //txtPath.Text = string.Empty;
            txtUrl.Text = "http://";
        }

        protected void cmdAddNext_Click(object sender, EventArgs e)
        {
            cmdAddNew_Click(null, null);
            cmdEmpty_Click(null, null);
        }

        //Nguyen add 
       
    }
}